﻿
 #include <d3dx9-dynamic-load/d3dx9_dynamic_load.h>
using  d3dx9_dynamic_load::CD3DX9_Dll_Loader;

 using namespace std;
 
	 //!  safe delete object
	#define SAFE_DEL_OBJ(p) { if(NULL != p) { delete   (p);  (p)=NULL; } }

	//!  safe release interface
	#define SAFE_REL(p) { if(NULL != p) { (p)->Release(); (p)=NULL; } }

//================================================================
int  main(int argc,  char* argv[])
{
  //    loader, may be class member .
  CD3DX9_Dll_Loader* loader = NULL;

  try 
  {   //  try load
    loader = new CD3DX9_Dll_Loader();
  }
  catch(...)
  {
    //  Error !((
    cout<< "Error load lib\n"<< endl;
    SAFE_DEL_OBJ(loader)
    return -1;
  }



  // for example we are create matrix stack interface
  ID3DXMatrixStack* pStack = NULL; 
   if FAILED(loader->D3DXCreateMatrixStack(0, &pStack))
   {
    //  error !! ((
     if(pStack)
	 {
	  SAFE_DEL_OBJ( loader);
	 }
      
       return  -1;
   }

   //   try usage ...
   pStack->Push();
   pStack->GetTop();
   pStack->Pop();

   //   release interface before unload dll
     SAFE_REL(pStack)


   //  remove loader. and  d3dx9 dll also automaticaly unloaded.
   SAFE_DEL_OBJ( loader);

  return 0;
}

